<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<link href="tablecloth/tablecloth.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="tablecloth/tablecloth.js"></script>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>

<?php
//Teamtauschbörse


include("inc/joomlaInt.php");
include("inc/inc_teamtausch.php");
include("inc/inc_configuration.php");

if ($jUser==0)
{
	echo "Bitte zuerst einloggen!";
}
else
{	
	if ($ttb==2)
	{
		$zeit=time();
		if (($zeit<$ttb_ab) || ($zeit>$ttb_bis)) 
		{
			echo "Bitte warte bis ",date("H:i",$fzeit)," Uhr am ",date("d.m.y",$fzeit),".";
			echo "<br><br>
			<center><a href='lima.php' class='button zurueck'>Zurück</a></center>";
			break;
		}
	}
	else
		if ($ttb==0)
		{
			echo "Die Teamtauschbörse ist zurzeit geschlossen!";
			break;
		}
		
		if ((($ttb==1) || (($ttb==2)) && (($zeit>$ttb_ab) || ($zeit<$ttb_bis))))
		{
			$ligaQ = "	SELECT
							b.id,
							u.username,
							t.name,
							b.bemerkung,
							t.id AS teamId,
							u.id AS uid,
							b.combinationId AS angcid
						FROM
							$tbl_ttb b,
							$tbl_users u,
							$tbl_combinations c,
							$tbl_teams t
						WHERE
							b.combinationId=c.id AND
							c.userId=u.id AND
							c.teamId=t.id AND
							c.active=1";
							
			$ligaQe = mysql_query($ligaQ);
			
			echo "<h1>Teamtauschbörse</h1><table><thead><tr><th colspan='3'></th></tr></thead>";
			
			$i=0;
			$t="";
			
			//echo $ligaQ;
			while ($li = mysql_fetch_object($ligaQe))
			{
				$bid = $li->id;
				$uid = $li->uid;
				$username = $li->username;
				$teamn = $li->name;
				$teamId = $li->teamId;
				$bemerkung = $li->bemerkung;
				$angcid = $li->angcid;
				
				echo "<tr>
						<td><img src='img/logos/",$teamId,".png' width='35' height='35' alt=''>$teamn</td>
						<td width='160px'>$bemerkung</td>
						<td>";
						
				$t.= " - " . $teamn;
				
				$abfrage = "	SELECT
									t.id AS teamId,
									t.name AS team,
									c.id AS cid
								FROM
									$tbl_ttb_a a,
									$tbl_combinations c,
									$tbl_teams t
								WHERE
									a.combinationId=c.id AND
									c.teamId=t.id AND
									a.ttbId=$bid";
									
				$ergebnis = mysql_query($abfrage);
				
				$anz=mysql_num_rows($ergebnis);
				
				if ($anz>0)
				{
					if ($jUser==$uid)
					{	
						echo '<form action="boerse_v.php" method="post">';
					}
					
					while ($row = mysql_fetch_object($ergebnis))
					{
						$ateamId=$row->teamId;
						$ateamN=$row->team;
						$acid=$row->cid;
						
						if ($jUser==$uid)
						{
							echo '<input type="radio" name="cId" value="',$acid,'">';
						}
						echo "<img src='img/logos/",$ateamId,".png' width='35' height='35' alt=''>";
						echo $ateamN;
						echo "<br>";
					}

					if ($jUser==$uid)
					{
						echo "	<input name='angebotId' type='hidden' value='$bid'>
								<input type='submit' value='Übernehmen' class='button eintragen'>
								</form>";
					}
					// echo "<thead><tr><th colspan='4'></th></tr></thead></table><br><input type='submit' value='Übernehmen' class='button eintragen'></form>";
				
				
				}
				//Anbieten
					$cQ = "SELECT c.id,name FROM $tbl_combinations c, $tbl_teams t WHERE c.userId=$jUser AND c.active=1 AND c.teamId=t.id";
					$cQe = mysql_query($cQ);
					$canz = mySQL_num_rows($cQe);
					
					if ($canz>0)
					{
						$c1o=mysql_fetch_object($cQe);
						$c1=$c1o->id;
						$c1n=$c1o->name;
						$comq="SELECT a.id, t.name, c.id AS cid FROM $tbl_ttb_a a, $tbl_teams t, $tbl_combinations c WHERE a.ttbId=$bid AND a.combinationId=c.id AND c.teamId=t.id AND (a.combinationId=$c1";
							
						if ($canz==2)
						{
							$c2o=mysql_fetch_object($cQe);
							$c2=$c2o->id;
							$c2n=$c2o->name;
							$comq.= " OR combinationId=$c2 )";
						}
						else
						{
							$comq.=")";
						}
						
						$comqE=mysql_query($comq);
						$comanz = mySQL_num_rows($comqE);
						
						$co1=0;
						
						if ($comanz>0)
						{
							if ($comanz==2)
							{
								$input="";
							}
							else
							{
								$co1o=mysql_fetch_object($comqE);
								$co1=$co1o->cid;
								
								if (($c1!=$co1) || ($c2!=$co1))
								{
									if ($c1!=$co1 && $c1!="")
									{
										$input="<form action='boerse_anbieten.php' method='post'>
												<select name='combination_angebot' size='1'>
													<option value='$c1'>$c1n</option>
												</select>
												<input name='angebotId' type='hidden' value='$bid'>
												<input type='submit' value='Anbieten' class='button teamwahl'>
												</form>";
									}
									else
										if ($c2!=$co1 && $c2!="")
										{
											$input="<form action='boerse_anbieten.php' method='post'>
													<select name='combination_angebot' size='1'>
														<option value='$c2'>$c2n</option>
													</select>
													<input name='angebotId' type='hidden' value='$bid'>
													<input type='submit' value='Anbieten' class='button teamwahl'>
													</form>";
										}
								}
								
							}
							
							echo $input;
							
						}
						else
						{
							$co1o=mysql_fetch_object($comqE);
							$co1=$co1o->cid;
							
								
							if (($comanz==0) && (($c1!=$angcid) && ($c2!=$angcid)))
							{
								$input="<form action='boerse_anbieten.php' method='post'>
											<select name='combination_angebot' size='1'>
												<option value='$c1'>$c1n</option>";
								if($canz==2)
								{
									$input.="<option value='$c2'>$c2n</option>";
								}
												
								$input.="	</select>
											<input name='angebotId' type='hidden' value='$bid'>
											<input type='submit' value='Anbieten' class='button teamwahl'>
											</form>";
											
								echo $input;
							}
						}
						
					}
					
				echo "</td>";
			}
			
			$input="";
			
			$cQ = "SELECT c.id,name FROM $tbl_combinations c, $tbl_teams t WHERE c.userId=$jUser AND c.active=1 AND c.teamId=t.id";
			$cQe = mysql_query($cQ);
			$canz = mySQL_num_rows($cQe);
			if ($canz>0)
					{
						$c1o=mysql_fetch_object($cQe);
						$c1=$c1o->id;
						$c1n=$c1o->name;
						
						$comq="	SELECT 	c.id AS cid 
								FROM 	$tbl_ttb a,
										$tbl_combinations c 
								WHERE 	a.combinationId=c.id AND 
										c.userId=$jUser AND
										(a.combinationId=$c1";
							
						
						if ($canz==2)
						{
							$c2o=mysql_fetch_object($cQe);
							$c2=$c2o->id;
							$c2n=$c2o->name;
							$comq.= " OR combinationId=$c2 )";
						}
						else
						{
							$comq.=")";
						}
						
						$comqE=mysql_query($comq);
						$comanz = mySQL_num_rows($comqE);
						
						$co1=0;
						
						if ($comanz>0)
						{
							$co1o=mysql_fetch_object($comqE);
							$co1=$co1o->cid;
							
							if ($comanz==2)
							{
								$eintragen="";
							}
							else
							{
								if (($canz==1) && ($c1==$co1))
								{
									$eintragen="";
								}
								else							
								{
									if ($c1!=$co1)
									{
										$que1 = "c1:$c1, c2:$c2, co1:$co1";
										$eintragen="<select name='combination_eintragen' size='1'>
													<option value='$c1'>$c1n</option>
												</select>";
									}
									else
									{
										if ($c2!=$co1)
										{
											$co2o=mysql_fetch_object($comqE);
											$co2=$co2o->cid;
											$co2n=$co2o->name;
											$eintragen="<select name='combination_eintragen' size='1'>
														<option value='$c2'>$c2n</option>
													</select>";
										}
									}
								}
								
							}
							
						}
						else
						{
							if ($comanz==0)
							{
								$eintragen="
											<select name='combination_eintragen' size='1'>
												<option value='$c1'>$c1n</option>";
								if($canz==2)
								{
									$eintragen.="<option value='$c2'>$c2n</option>";
								}
												
								$eintragen.="	</select>";
							}
						}
						
					}
			
			echo"</tr><thead><tr><th colspan='3'></th></tr></thead></table><br><br>";
			
			if ($eintragen!="")
			{
				echo "	<form action='boerse_eintragen.php' method='post'><table>
							<thead><tr><th colspan='2'>Team anbieten</th></tr></thead>
							<tr>
								<td>$eintragen</td>
								<td><textarea name='bezeichnung' cols='50' rows='10' wrap='soft'></textarea></td>
							</tr>
							<tr>
								<td></td>
								<td><input type='submit' value='Anbieten' class='button teamwahl'></td>
							</tr>
							<thead><tr><th colspan='2'></th></tr></thead>
						</table>
						</form>";
			}
			echo "<center><a href='lima.php' class='button zurueck'>Zurück</a></center>";
			
		}
		else
		{
			echo "Kein Teamtausch mehr erlaubt";
		}
}
?>

</body>
</html>